ERR_CLASSNAME="icon_form-error form_icons";
SUCCESS_CLASSNAME="icon_form-ok form_icons";
LOAD_CLASSNAME="icon_form-loading form_icons";
ERR_TAG="span"; //span, div or p
HAS_SUCCESS=true;
POSITION="after";
//TODO match verifcation, eg.: password verification

var Form = {  
  /*
    Fields = [{'id':objetct_id, 'validate':[number, required, ...]}]
  */
  fields:[],//on submit validation fields
  blur_fields:[],//validate on field blur
  ajax_fields:[],//for fields wich need ajax validation live username and email verification
   
  validators:{ 
	  number: {
		  reg_exp: /^[-]?\d+(\.\d+)?$/,
      err_msg:"Only Numbers"
	  },

	  digits: {
		  reg_exp: /^[-]?\d+(\.\d+)?$/,
      err_msg:"Only Numeral values"
	  },

	  email: {
		  reg_exp: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
      err_msg:"Invalid Email"
	  },

	  alpha: {
		  err_msg: "This field must contain only letters.",
		  reg_exp: /^[a-zA-z\s]+$/
	  },

	  alphanum: {
		  err_msg: "This field must contain only letters or numbers.",
		  reg_exp: /^[a-zA-Z0-9]+$/
	  }
  },

  __init__ : function(){
  
      this.blur_fields.each(
        function(field, index){
           Event.observe(field.id, "blur", function(event){
                Form.check(event, field);
           });
        }
      );
  
      this.ajax_fields.each(function(field, index){
           Event.observe(field.id, "blur", function(event){
                Form.check_ajax(event, field);
           });
        }
      );

  },

  check : function(event, field){
    field.validate.each(function(validation, index){
        tag_id = ERR_TAG+"_"+field.id;

        if ($(tag_id)==null){
          ele_tag = new Element(ERR_TAG, {class:ERR_CLASSNAME, id:tag_id});
          $(field.id).insert({after:ele_tag});
        }

        if (validation=="required"){
            if($(field.id).value.empty()){
              $(tag_id).className=ERR_CLASSNAME;
              $(tag_id).insert("Campo deve ser preenchido");
            }else{
               HAS_SUCCESS ? ($(tag_id).className=SUCCESS_CLASSNAME) : $(tag_id).remove();
            }

        }else{
            validator = Form.validators[validation];
            if (!validator.reg_exp.test( $(field.id).value ) ){
              $(tag_id).className=ERR_CLASSNAME;
              $(tag_id).insert(validator.err_msg);
            }else{
              HAS_SUCCESS ? ($(tag_id).className=SUCCESS_CLASSNAME) : $(tag_id).remove();                
            }
        }
    });
  },

  check_ajax : function(event, field){
      url= field.url+"?"+field.params+"="+$(field.id).value;
  
      tag_id = ERR_TAG+"_ajax_"+field.id;
      if ($(tag_id)==null){
        ele_tag = new Element(ERR_TAG, {class:LOAD_CLASSNAME, id:tag_id});
      }


    ajax = new Ajax.Request(url,
      {onSuccess:function(response){Form.check_response(response, field)},
       onLoading:$(field.id).insert({after:ele_tag})
      });
  },

  check_response:function(response,field){
      tag_id = ERR_TAG+"_ajax_"+field.id;
      json = response.responseJSON;

      if ($(tag_id)==null){
        ele_tag = new Element(ERR_TAG, {class:ERR_CLASSNAME, id:tag_id});
        $(field.id).insert({after:ele_tag});
      }

      if (json.status){
          $(tag_id).className=SUCCESS_CLASSNAME;//(json.msg);
      }else{
          $(tag_id).className=ERR_CLASSNAME ;
      }
  }
}
